<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/html; charset=UTF-8"
		import="org.hibernate.SQLQuery,
				org.hibernate.Query" %>
<%
	Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();

	//SQLQuery query = hSession.createSQLQuery("SELECT B.* FROM APP.BOOK B WHERE EXISTS (SELECT 1 FROM APP.ORDERLINE O WHERE O.book_id = B.id) ORDER BY (SELECT SUM(O.quantity) FROM APP.ORDERLINE O WHERE O.book_id = B.id) DESC");
	//query.addEntity(Book.class);
	
	Query query = hSession.createQuery("FROM Book ORDER BY orders.size DESC");
	query.setMaxResults(5);
	Collection<Book> bestSellers = query.list();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
	<title>Bookstore</title>
	<%@include file="include.jsp" %>
</head>

<body>
	<%@include file="commun.jsp" %>
	
	<div class="contenuAvecMenu contenuAvecOutils">
		<h2>Meilleures ventes</h2>
		<div class="boite bleue">
			<ol>
				<% for (Book book : bestSellers) { %>
					<li>
						<a href="category.jsp?id=<%=book.getCategory().getId()%>#BOOK<%=book.getId()%>"><%=book%></a>
						<em>
							(<%=book.getCategory()%>)
						</em>
					</li>
				<% } %>
			</ol>
		</div>
	</div>
</body>
</html>
